package com.example.simplecalculator;

import java.text.DecimalFormat;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	Button btnAddition, btnSubtraction; //create Button variable // treat it as variable
	EditText etFirstNumber, etSecondNumber; //create EditText variable
	TextView tvResult; //create TextView variable
	double firstNumber, secondNumber, result; //crate double variable
	String output; //crate string variable

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnAddition = (Button) findViewById(R.id.btnAdd); //link Button variable with XML Button// id is from XML file
		btnSubtraction = (Button) findViewById(R.id.btnSub);
		etFirstNumber = (EditText) findViewById(R.id.etFn);//link EditText variable with XML EditText 
		etSecondNumber = (EditText) findViewById(R.id.etSn);
		tvResult = (TextView) findViewById(R.id.tvR);//link TextView variable with XML TextView

		btnAddition.setOnClickListener(new OnClickListener() { //Assign Button to setOnClickListener
			public void onClick(View v) {
				if (etFirstNumber.length() > 0) { //check whether user put any value in the filed
					firstNumber = Double.parseDouble(etFirstNumber.getText()//if true, assign value to firstNumber variable
							.toString());//from String to double then assign to firstNumber variable
				} else
					Toast.makeText(getBaseContext(), //if false, display Toast to user
							"Please add some number in the first field",
							Toast.LENGTH_SHORT).show();
				if (etSecondNumber.length() > 0) { //similar to first number//checking the input
					secondNumber = Double.parseDouble(etSecondNumber.getText()
							.toString());
				} else
					Toast.makeText(getBaseContext(),
							"Please add some number in the second field",
							Toast.LENGTH_SHORT).show();
				
				result = firstNumber + secondNumber; //value addition
				DecimalFormat round = new DecimalFormat("###.##");//formating double value to 2 decimal
				output = round.format(result); //format the result
				tvResult.setText(output); //display the formatted string value
			}
		});

		btnSubtraction.setOnClickListener(new OnClickListener() { //Assign Button to setOnClickListener//in this case, subtraction button
			public void onClick(View v) {
				if (etFirstNumber.length() > 0) {
					firstNumber = Double.parseDouble(etFirstNumber.getText()
							.toString());
				} else
					Toast.makeText(getBaseContext(),
							"Please add some number in the first field",
							Toast.LENGTH_SHORT).show();
				if (etSecondNumber.length() > 0) {
					secondNumber = Double.parseDouble(etSecondNumber.getText()
							.toString());
				} else
					Toast.makeText(getBaseContext(),
							"Please add some number in the second field",
							Toast.LENGTH_SHORT).show();
				result = firstNumber - secondNumber; //all same as above listener. only this line is for subtraction.
				DecimalFormat round = new DecimalFormat("###.##");
				output = round.format(result);
				tvResult.setText(output);

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
}
